package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.Admin_he;
import com.tr.rentcar.model.bean.Adminrole;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;

public class AdminroleDao implements ResultHandler<Adminrole>{
	private MySqlDb db=MySqlDb.getInstance();

	public List<Adminrole> findall() throws SQLException{
		String sql="select * from admin_role";
		return db.executeQuery(sql, this);
	}
	/**
	 * 添加一个角色
	 * @param arname
	 * @return
	 * @throws SQLException
	 */
	public boolean save(String arname) throws SQLException{
		String sql="insert into admin_role(ar_name) values(?) ";
		return db.executeUpdate(sql,new GlobalParamHandler(arname))>0;
	}
	
	/**
	 * 删除根据id
	 */
	public boolean deleterole(Integer ar_id) throws SQLException{
		String sql="delete from admin_role where ar_id=?";
		return db.executeUpdate(sql,new GlobalParamHandler(ar_id))>0;
	}
	/**
	 * 更新一个角色
	 * @param arname
	 * @return
	 * @throws SQLException
	 */
	public boolean update(String arname,Integer arid) throws SQLException{
		String sql="update admin_role set ar_name=? where ar_id=?";
		return db.executeUpdate(sql,new GlobalParamHandler(arname,arid))>0;
	}
	
	public Adminrole findallbyname(String arname) throws SQLException{
		String sql="select * from admin_role where ar_name=?";
		return  db.queryUninResult(sql, new GlobalParamHandler(arname),this);
	}
	@Override
	public Adminrole doHander(Map<String, Object> row) {
		Adminrole Adrole=new Adminrole();
		Adrole.setAr_id(Integer.parseInt(row.get("ar_id")+""));
		Adrole.setAr_name(row.get("ar_name")+"");
		return Adrole;
	}

}
